//
//  ChildRecordController2.m
//  Yearly Plan
//
//  Created by admin on 2019/2/27.
//  Copyright © 2019年 admin. All rights reserved.
//

#import "ChildRecordController2.h"
#import "WaitFinishCell.h"
#import "EidtInfoViewController.h"
@interface ChildRecordController2 ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic)NSMutableArray *dataArray;
@property(nonatomic, strong)UIView *bgView;
@end

@implementation ChildRecordController2

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSMutableArray *array = (NSMutableArray *)[Defaults objectForKey:weiwancheng];
    self.dataArray = [NSMutableArray arrayWithArray:array];
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (self.tableView) {
        [self.tableView reloadData];
    }
    if (self.dataArray.count!=0) {
        [self.bgView removeFromSuperview];
    } else {
        [self showHolderView];
    }
}

-(void)showHolderView {
    self.bgView = [UIView new];
    //self.bgView.backgroundColor = [UIColor redColor];
    [self.tableView addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.tableView);
        make.size.mas_equalTo(CGSizeMake(kWidth(300), kHeight(400)));
    }];
    
    UIImageView *img = [UIImageView new];
    [img setImage:[UIImage imageNamed:@"pic_01"]];
    [self.bgView addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView);
        make.centerX.equalTo(self.bgView);
    }];
    
    UIButton *addBtn = [UIButton new];
    addBtn.backgroundColor = HEX_COLOR(0xF7CE47);
    [addBtn setTitle:@"Creates a new entry in the to-do" forState:UIControlStateNormal];
    [addBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    addBtn.titleLabel.font = RegularDefaultFont;
    addBtn.layer.cornerRadius = kWidth(8);
    [addBtn addTarget:self action:@selector(pushToAddInfoVc) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:addBtn];
    [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(img.mas_bottom).offset(kHeight(5));
        make.centerX.equalTo(self.bgView);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-kWidth(60), kHeight(30)));
    }];
}

-(void)pushToAddInfoVc {
    NSLog(@"push");
    EidtInfoViewController *vc = [EidtInfoViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blueColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kHeight(128);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WaitFinishCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cells"];
    if (!cell) {
        cell = [[WaitFinishCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cells"];
    }
    NSDictionary *dic = self.dataArray[indexPath.row];
    cell.infoLab.text = [NSString stringWithFormat:@"%@：  %@",dic[@"time"],dic[@"name"]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.eidtBtn.tag = indexPath.row+10000;
    [cell.eidtBtn addTarget:self action:@selector(clickEditBtn:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

-(void)clickEditBtn:(UIButton*)sender {
    NSInteger index = sender.tag-10000;
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic = self.dataArray[index];
    NSMutableArray *yiwanchengArray = (NSMutableArray*)[Defaults objectForKey:yiwancheng];
    NSMutableArray *yijingArr = [NSMutableArray arrayWithArray:yiwanchengArray];
    [yijingArr addObject:dic];
    [Defaults setObject:yijingArr forKey:yiwancheng];
    [self.dataArray removeObjectAtIndex:index];
    [Defaults setObject:self.dataArray forKey:weiwancheng];
    [self.tableView reloadData];
    [self showHint:@"Finish the task"];
    if (self.dataArray.count!=0) {
        [self.bgView removeFromSuperview];
    } else {
        [self showHolderView];
    }
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"RemovePlaceHolderView" object:nil];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    EidtInfoViewController *vc = [EidtInfoViewController new];
    if ([self.dataArray[indexPath.row] isKindOfClass:[NSDictionary class]]) {
        NSMutableDictionary *dic = self.dataArray[indexPath.row];
        vc.name = dic[@"name"];
        vc.time = dic[@"time"];
        vc.content = dic[@"content"];
    }
    [self.navigationController pushViewController:vc animated:YES];
}



@end
